﻿/**
 * Created by json on 2016/8/18.
 */
$.runningScene = null;
$.is_FirstLogin = false;
$.vecServerList = [];

$.defaultWebsocketUrl = null;

var mj4 = mj4 || {};
var mj2 = mj2 || {};
var niuniu = niuniu || {};
var zjh = zjh || {};
var xlch = xlch || {};
var sg = sg || {};
var xjmj = xjmj || {};
var ynmj = ynmj || {};
var jhmj = jhmj || {};
var csmj = csmj || {};

dwc.DT_Data_Login={
    m_uiDTScene : null,

    InitData : function() {
        var that = this;
        this.m_bIsLogin = false;
        that.vecWebSocket = {};
        that.LastConnectTime = 0;
    },
    loadRemoteConfig : function()
    {
        var bundleID = $.getBundleID();

        var v1AppStoreTimeStamp = 20161111;

        var _url = "http://zjmj.cjpoker.com/app/ios.php?bundle="+bundleID +"&v1="+ v1AppStoreTimeStamp + "&os=ios";
        if(cc.sys.os == cc.sys.OS_ANDROID)
        {
            var _url = "http://zjmj.cjpoker.com/app/ios.php?bundle="+bundleID +"&v1="+ v1AppStoreTimeStamp + "&os=android";
        }
        if(cc.sys.os == cc.sys.OS_WINDOWS)
        {
            var _url = "http://zjmj.cjpoker.com/app/ios.php?bundle="+bundleID +"&v1="+ v1AppStoreTimeStamp + "&os=windows";
        }

        //网络请求
        dwc.GX_NetWorkManager.SendMessage(_url,
            function (_msg) {
                try {
                    var message=JSON.parse(_msg);
                    if(message && message["bAppStore"])
                    {
                        $.setItem("bAppStore",message["bAppStore"]);
                        $.setItem("bAliPay",message["bAliPay"]);
                        $.setItem("bWeChat",message["bWeChat"]);
                    }
                }catch (e)
                {
                    cc.log(e.name + " : " + e.message);
                }
            });
    },

    isConnected : function()
    {
        if(this.websocketManager)
        {
            return this.websocketManager.connected();
        }
        return false;
    },
    Disconnect : function(){
        var that=this;
        if(that.websocketManager) {
            that.websocketManager.Close();
            that.websocketManager = null;
        }
        that.m_bIsLogin = false;
    },
    ConnectServer : function(_url,_callback)
    {
        var self = this;
        //$.defaultWebsocketUrl = "ws://114.215.197.62:8001";//"ws://127.0.0.1:9100";//"ws://mj2.cjpoker.com:9100";//"ws://127.0.0.1:9100";//
        //$.defaultWebsocketUrl = "ws://mj.cjpoker.com:8001";

        _url = "ws://192.168.31.123:9100";
        //_url = "ws://127.0.0.1:9100";
        //_url = "ws://niuniu.cjpoker.com:9100";
        //_url = "ws://115.29.172.202:9200";
        //_url = "ws://115.29.170.185:9500";

        self.vecWebSocket[_url] = createWebSocket(_url);
        if(self.vecWebSocket[_url])
        {
            self.vecWebSocket[_url].AddOpenListener(function(url)
            {
                if(!$.defaultWebsocketUrl) {
                    $.defaultWebsocketUrl = url;
                }
                if(self.websocketManager == null)
                {
                    this.AddListener(self, self.OnWebSocketMessage);
                    self.websocketManager = this;

                    if(_callback){
                        _callback();
                    }
                    $.setItem("default_server_win",url);
                }else{
                    if(self.websocketManager != this)
                    {
                        this.Close();
                    }
                }
            });
        }
    },
    SendMessage : function(__cmd, __data){
        if(this.websocketManager){
            this.websocketManager.Send(__cmd, __data);
        }
    },    
    DoLogin: function (_loginSuccessCallFunc, _username, _pass) {
        this.loginSuccessCallFunc=_loginSuccessCallFunc;
        var self = this;

        this.sendLogin(_username,_pass);

        //dwc.DT_Data_Login.websocketManager.AddListener(this,self.OnWebSocketMessage);
    },
    QQLogin: function (_loginSuccessCallFunc, obj_data) {
        var that = this;
        this.loginSuccessCallFunc=_loginSuccessCallFunc;
        var self = this;

        var UID=cc.sys.localStorage.getItem("QQUID");
        var device = undefined;
        if(!device){
            device = cc.sys.localStorage.getItem("device");
        }
        if(device == null || device== undefined || device == "") {
            try {
                if (cc.sys.os == cc.sys.OS_IOS) {
                    var uuid = jsb.reflection.callStaticMethod(
                        "AppController",
                        "GetSystemUUID");
                    device = uuid;
                }
                if (cc.sys.os == cc.sys.OS_ANDROID) {
                    var uuid = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "GetGUID", "()Ljava/lang/String;");
                    device = uuid;
                    cc.log("device : " + device);
                }
            } catch (e) {
                cc.log(e.name + " : " + e.message);
            }
        }
        if(device == null || device== undefined || device == "")
        {
            device = "" + new Date().getTime();
            cc.sys.localStorage.setItem("device",device);
        }

        device = dwc.md5(device).toUpperCase();

        cc.log("device : " + device);

        var hardware = "unknown";
        if(cc.sys.os == cc.sys.OS_IOS) {
            hardware = "ios";
            var frameSize = cc.view.getFrameSize();
            if(frameSize.width == 768 || frameSize.width == 1024 || frameSize.width == 2048 || frameSize.width == 1536)
            {
                hardware = "ipad";
            }
            if(frameSize.width == 640 || frameSize.width == 1136 || frameSize.width == 750 || frameSize.width == 1334 || frameSize.width == 1242 || frameSize.width == 2208)
            {
                hardware = "iphone";
            }
        }
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            hardware = "android";
        }
        var person= {
            type: 2,
            uid: UID,
            platform : "QQ",
            user_id: obj_data.userid,
            user_nick: obj_data.nickname,
            user_icon: obj_data.usericon,
            user_gender : obj_data.usergender,
            oauth_token: obj_data.token,
            device: device,
            hardware : hardware,
        };
        that.SendMessage("hall_qqlogin",person);
    },
    WXLogin: function (_loginSuccessCallFunc, obj_data) {
        var that = this;
        this.loginSuccessCallFunc=_loginSuccessCallFunc;
        var self = this;


        var UID=cc.sys.localStorage.getItem("WXUID");


        var device = undefined;
        if(!device){
            device = cc.sys.localStorage.getItem("device");
        }
        if(device == null || device== undefined || device == "") {
            try {
                if (cc.sys.os == cc.sys.OS_IOS) {
                    var uuid = jsb.reflection.callStaticMethod(
                        "AppController",
                        "GetSystemUUID");
                    device = uuid;
                }
                if (cc.sys.os == cc.sys.OS_ANDROID) {
                    var uuid = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "GetGUID", "()Ljava/lang/String;");
                    device = uuid;
                    cc.log("device : " + device);
                }
            } catch (e) {
                cc.log(e.name + " : " + e.message);
            }
        }
        if(device == null || device== undefined || device == "")
        {
            device = "" + new Date().getTime();
            cc.sys.localStorage.setItem("device",device);
        }
        if (cc.sys.os == cc.sys.OS_IOS) {
            device = dwc.md5(device).toUpperCase();
        }

        cc.log("device : " + device);

        if(UID == null || UID == "" || UID== undefined)
        {
            UID = 0;
        }else{
            try{
                UID = parseInt(UID);
            }catch(e) {
                UID = 0;
            }
        }

        var hardware = "unknown";
        if(cc.sys.os == cc.sys.OS_IOS) {
            hardware = "ios";
            var frameSize = cc.view.getFrameSize();
            if(frameSize.width == 768 || frameSize.width == 1024 || frameSize.width == 2048 || frameSize.width == 1536)
            {
                hardware = "ipad";
            }
            if(frameSize.width == 640 || frameSize.width == 1136 || frameSize.width == 750 || frameSize.width == 1334 || frameSize.width == 1242 || frameSize.width == 2208)
            {
                hardware = "iphone";
            }
        }
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            hardware = "android";
        }
        var person= {
            type: 2,
            uid: UID,
            platform : "WX",
            user_id: obj_data.userid,
            user_nick: obj_data.nickname,
            user_icon: obj_data.usericon,
            user_gender : obj_data.usergender,
            oauth_token: obj_data.token,
            union_id: obj_data.unionid,
            device: device,
            hardware : hardware,
        };
        that.SendMessage("hall_wxlogin",person);
    },

    SendFangGuanRoomList: function (callBack) {
        this.fangGuancallBack = callBack;
        this.SendMessage("FGRoomList");
    },

    OnWebSocketMessage: function (_msg) {
        var that=this;
        if(_msg){

            if (_msg.command&&_msg.command=="onclose"){
                this.websocketManager = null;
            }else if (_msg.command&&_msg.command=="onerror"){
                this.websocketManager = null;
            }else if(_msg.command&&_msg.command=="hall_loginlottery"){

            }else if (_msg.command&&_msg.command=="hall_serverlist"){

            }else if(_msg.command&&_msg.command=="hall_notice"){

            }else if(_msg.command&&_msg.command=="hall_register") {
                //that._register(_msg);
            }else if(_msg.command&&_msg.command=="hall_login") {
                that._login(_msg);
            }else if(_msg.command&&_msg.command=="hall_qqlogin") {
                that._qqlogin(_msg);
            }else if(_msg.command&&_msg.command=="hall_wxlogin") {
                that._wxlogin(_msg);
            }else if(_msg.command&&_msg.command=="hall_updateuser") {
                that._updateuser(_msg);
            }else if(_msg.command&&_msg.command=="hall_create_room") {
                that._create_room(_msg);
            }else if(_msg.command&&_msg.command=="hall_join_room") {
                that._join_room(_msg);
            }else if(_msg.command&&_msg.command=="hall_dismiss_room") {
                that._dismiss_room(_msg);
            }else if(_msg.command&&_msg.command=="hall_user_room") {
                that._user_room(_msg);
            }else if(_msg.command&&_msg.command=="hall_user_avatar") {
                that._user_avatar(_msg);
            }else if(_msg.command&&_msg.command=="hall_share_timeline") {
                that._share_timeline(_msg);
            }else if(_msg.command&&_msg.command=="hall_tongzhi") {
                that._tongzhi(_msg);
            }else if(_msg.command&&_msg.command=="hall_chip") {
                that._userchip(_msg);
            }else if(_msg.command&&_msg.command=="hall_bind_agent") {
                that._bind_agent(_msg);
            }else if(_msg.command&&_msg.command=="hall_exception") {
                //that._exception(_msg);
            }else if (_msg.command&&_msg.command=="hall_paysuccess"){
                that._paysuccess(_msg);
            }else if (_msg.command&&_msg.command == "hall_convert_gem2coin") {
                that._convertsuccess(_msg);
            }else if (_msg.command&&_msg.command == "hall_transfer_coin") {
                that._transfer_coin(_msg);
            }else if (_msg.command&&_msg.command == "FGRoomList") {
                if(_msg&&that.fangGuancallBack){
                    that.fangGuancallBack(_msg);
                }

            }
        }
    },
    SendConnect : function()
    {
        var that = this;
        var obj = {};
        if(that.websocketManager)
        {
            that.SendMessage("connect",obj);
        }else{
            dwc.DT_Data_Login.ConnectServer($.defaultWebsocketUrl,function()
            {
                var obj = {};
                that.SendMessage("connect",obj);
            });
        }
    },
    sendUserAvatar : function()
    {
        var that = this;
        var obj = {};
        if(that.websocketManager)
        {
            that.SendMessage("hall_user_avatar",obj);
        }
    },
    sendLogin : function(device,password){
        var that = this;
        var UID=cc.sys.localStorage.getItem("YKUID");

        if(!password){
            password=cc.sys.localStorage.getItem("Password");
        }
        if(!device){
            device = cc.sys.localStorage.getItem("device");
        }
        //device = "123456789x";
         device = "123456789" + Math.floor(Math.random()*100+1);

        if(device == null || device== undefined || device == "") {
            try {
                if (cc.sys.os == cc.sys.OS_IOS) {
                    var uuid = jsb.reflection.callStaticMethod(
                        "AppController",
                        "GetSystemUUID");
                    device = uuid;
                }
                if (cc.sys.os == cc.sys.OS_ANDROID) {
                    var uuid = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "GetGUID", "()Ljava/lang/String;");
                    device = uuid;
                    cc.log("device : " + device);
                }
            } catch (e) {
                cc.log(e.name + " : " + e.message);
            }
        }
        if(device == null || device== undefined || device == "")
        {
            device = "" + new Date().getTime();
            cc.sys.localStorage.setItem("device",device);
        }

        device = dwc.md5(device).toUpperCase();

        cc.log("device : " + device);
        if(password == null || password == "" || password== undefined)
        {
            password = "61B6FD19ABB817653E3B4048C3259E5";
        }
        if(UID == null || UID == "" || UID== undefined)
        {
            UID = 0;
        }else{
            try{
                UID = parseInt(UID);
            }catch(e) {
                UID = 0;
            }
        }

        var hardware = "unknown";
        if(cc.sys.os == cc.sys.OS_IOS) {
            hardware = "ios";
            var frameSize = cc.view.getFrameSize();
            if(frameSize.width == 768 || frameSize.width == 1024 || frameSize.width == 2048 || frameSize.width == 1536)
            {
                hardware = "ipad";
            }
            if(frameSize.width == 640 || frameSize.width == 1136 || frameSize.width == 750 || frameSize.width == 1334 || frameSize.width == 1242 || frameSize.width == 2208)
            {
                hardware = "iphone";
            }
        }
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            hardware = "android";
        }

         // device = "123456789" + Math.floor(Math.random()*100+1);
         // UID = Math.floor(Math.random()*5000+1);//100070;
        //device = "oZ8WJw1P_xc_JBihl4u2iD_IBX_M";
        var person= {
            type: 2,
            uid: UID,
            account: device,
            device: device,
            hardware : hardware,
            pwd: password,
        };
        that.SendMessage("hall_login",person);

    },

    _login : function(__msg){
        if (__msg.command != "hall_login"){
            return;
        }
        try
        {
            this.m_bIsLogin = true;
            dwc.Myself.InitData(__msg);
        }catch(e)
        {
            cc.log(e.name + ":" + e.message);
        }
        try
        {
            $.uid = __msg.m_objUser.m_lUid;
            cc.sys.localStorage.setItem("YKUID",__msg.m_objUser.m_lUid);//?
            this.loginSuccessCallFunc();
            cc.log("登录成功");
        }catch(e)
        {
            cc.log(e.name + ":" + e.message);
        }
    },
    _qqlogin : function(__msg){
        if (__msg.command != "hall_qqlogin"){
            return;
        }
        try
        {
            this.m_bIsLogin = true;
            dwc.Myself.InitData(__msg);
        }catch(e)
        {
            cc.log(e.name + " : " + e.message);
        }
        try
        {
            $.uid = __msg.m_objUser.m_lUid;
            cc.sys.localStorage.setItem("QQUID",__msg.m_objUser.m_lUid);//?
            this.loginSuccessCallFunc();
            cc.log("登录成功");
        }catch(e)
        {
            cc.log(e.name + ":" + e.message);
        }
    },
    _wxlogin : function(__msg){
        if (__msg.command != "hall_wxlogin"){
            return;
        }
        try
        {
            this.m_bIsLogin = true;
            dwc.Myself.InitData(__msg);
        }catch(e)
        {
            cc.log(e.name + " : " + e.message);
        }

        try
        {
            $.uid = __msg.m_objUser.m_lUid;
            cc.log("$.uid : " + $.uid);
            cc.sys.localStorage.setItem("WXUID",$.uid);//?
        }catch(e)
        {
            cc.log(e.name + ":" + e.message);
        }

        this.loginSuccessCallFunc();
        cc.log("登录成功");

    },
    sendUpdateUserInfo : function(__userInfo){
        var that = this;
        that.SendMessage("hall_updateuser",__userInfo);
    },
    _updateuser : function(__msg){
        try
        {
            if (__msg.command != "hall_updateuser"){
                return;
            }
            cc.log("修改个人信息" + __msg);
            if (__msg.m_sNickName) {
                dwc.Myself.GetData().SetNickName(__msg.m_sNickName);
            }

            dwc.Myself.GetData().SetSex(parseInt(__msg.m_iSex));
            dwc.Myself.GetData().SetSignName(__msg.m_sSign);
            if(__msg.m_iAvatar){
                __msg.m_iAvatar = parseInt(__msg.m_iAvatar);
                dwc.Myself.GetData().SetAvatar(__msg.m_iAvatar);
            }else{
                dwc.Myself.GetData().SetAvatar(0);
            }

            cc.eventManager.dispatchCustomEvent("CHANGE_PLAYER_INFO");
        }catch(e)
        {
            cc.log(e.name + ":" + e.message);
        }
    },
    sendUserChip : function(){
        try
        {
            var that = this;
            var obj = {};
            if(that.websocketManager)
            {
                that.SendMessage("hall_chip",obj);
            }
        }catch(e)
        {
            cc.log(e.name + ":" + e.message);
        }
    },
    sendBindAgent : function(agent){
        var that = this;
        var obj = {};
        obj["agent"] = agent;
        if(that.websocketManager)
        {
            that.SendMessage("hall_bind_agent",obj);
        }
    },
    sendUserRoom : function(){
        var that = this;
        var obj = {};
        if(that.websocketManager)
        {
            that.SendMessage("hall_user_room",obj);
        }
    },
    SendCreateRoom : function(room_type,param,isFangGuan)
    {
        var that = this;
        var obj ={
            room_type : room_type,
            room_param : param,
            room_fg : isFangGuan
        };
        if(that.websocketManager)
        {
            that.SendMessage("hall_create_room",obj);
        }
    },
    SendReturnRoom : function()
    {
        var that = this;
        var obj ={
            room_id : 0,
        };
        if(that.websocketManager)
        {
            that.SendMessage("hall_return_room",obj);
        }
    },
    SendJoinRoom : function(roomid)
    {
        var that = this;
        var obj ={
            room_id : roomid,
        };
        if(that.websocketManager)
        {
            that.SendMessage("hall_join_room",obj);
        }
    },
    SendDismissRoom : function(room_id)
    {
        var that = this;
        var obj ={
            room_id : room_id,
        };
        if(that.websocketManager)
        {
            that.SendMessage("hall_dismiss_room",obj);
        }
    },
    SendShareTimeLine : function()
    {
        var that = this;
        var obj ={
        };
        if(that.websocketManager)
        {
            that.SendMessage("hall_share_timeline",obj);
        }
    },
    _create_room : function(__msg)
    {
        if(__msg.room_type == "XLCH")
        {
			var uid = __msg.uid;
			var cbResult = __msg.cbResult;
            if(cbResult == 0)
            {
                if(uid == dwc.Myself.GetData().GetUid())
                {
                    if(!this.m_uiGameLayer) {
                        cc.director.runScene(new xlch.mj_uiGameScene());
                    }
                }
            }else{
                if($.runningScene)
                {
                    var alert=new dwc.PopupAlert("您无法创建房间，请检查是否有足够的房卡!",function()
                    {
                        cc.director.runScene(new dwc.LoginScene());
                    });
                    $.runningScene.addChild(alert);
                    alert.Show();
                }
            }
        }
        if(__msg.room_type == "GDMJ")
        {

        }
    },
    _return_room : function(__msg)
    {
        try
        {
            var uid = __msg.uid;
            var cbResult = __msg.cbResult;
        }catch (e)
        {
            cc.log(e.name + ":" + e.message);
        }
    },
    _dismiss_room : function(__msg) {

        try
        {
            var uid = __msg.uid;
            var cbResult = __msg.cbResult;
            var room_id =  __msg.room_id;
            if(cbResult == 0 )
            {
                cc.eventManager.dispatchCustomEvent("FG_DISMISS_ROOM");
            }else{
                if(cbResult == 11)
                {
                    var alert=new dwc.PopupAlert("房间内有人，不能解散房间");
                    $.runningScene.addChild(alert);
                    alert.Show();
                }
                if(cbResult == 12)
                {
                    var alert=new dwc.PopupAlert("游戏已经开始，不能解散房间");
                    $.runningScene.addChild(alert);
                    alert.Show();
                }
            }
        }catch (e)
        {
            cc.log(e.name + ":" + e.message);
        }
    },
    _join_room : function(__msg) {
        var uid = __msg.uid;
        var cbResult = __msg.cbResult;
        var room_id =  __msg.room_id;
        if (cbResult == 0) {
            var room_type = __msg.room_type;

            this.gotoStage(room_type);
        }else{
            try {
                if($.runningScene)
                {
                    if(cbResult == 12 || cbResult == 13)
                    {
                        var alert=new dwc.PopupAlert("房间已满，不能加入房间");
                        $.runningScene.addChild(alert);
                        alert.Show();
                    }else if(cbResult == 11)
                    {
                        var alert=new dwc.PopupAlert("房卡不足，请检查房间类型");
                        $.runningScene.addChild(alert);
                        alert.Show();
                    }else{
                        if(room_id > 0)
                        {
                            var alert=new dwc.PopupAlert("您输入的房间号" + room_id +"不存在，请重新输入");
                            $.runningScene.addChild(alert);
                            alert.Show();
                        }else{
                            var alert=new dwc.PopupAlert("您的房间不存在，请刷新状态");
                            $.runningScene.addChild(alert);
                            alert.Show();
                        }
                    }
                }
            }catch (e)
            {
                cc.log(e.name + ":" + e.message);
            }
        }
    },
    _user_room : function(__msg){
        var obj = {};
        obj.uid = __msg.uid;
        obj.room_id = __msg.room_id;
        if(obj.room_id > 0)
        {
            dwc.Myself.SetMyRoom(obj);
            if($.is_FirstLogin)
            {
                $.is_FirstLogin = false;
                this.SendReturnRoom();
            }
        }else{
            dwc.Myself.SetMyRoom(null);
        }
        try {
            this.m_uiDTScene._checkMyRoom();
        }catch (e)
        {
            cc.log(e.name + ":" + e.message);
        }
    },

    _user_avatar : function(__msg){
        var avatar_url = __msg.avatar_url;
        dwc.Myself.GetData().SetAvatarUrl(avatar_url);
        cc.eventManager.dispatchCustomEvent("CHANGE_AVATAR");
    },
    _share_timeline : function(__msg){
        setTimeout(function()
        {
            try
            {
                var obj = {};
                obj.cmd = "hall_share_timeline";
                obj.gem = __msg.m_lTotalGem;
                obj.text = "感谢您的分享，赠送钻石1颗已经到帐";
                if(__msg.m_lTotalGem)
                    dwc.Myself.GetData().SetGem(__msg.m_lTotalGem);
                //cc.eventManager.dispatchCustomEvent("TONGZHI",obj);
                if($.runningScene)
                {
                    $.disconnect_notice = 1;
                    var alert=new dwc.PopupAlert("感谢您的分享，赠送钻石1颗已经到帐");
                    $.runningScene.addChild(alert);
                    alert.Show();
                    cc.eventManager.dispatchCustomEvent("CHANGE_PLAYER_INFO");
                }
            }catch (e)
            {
                cc.log(e.name + ":" + e.message);
            }
        },8000);
    },
    _tongzhi : function(__msg){
        try
        {
            var text = __msg.szContent;

            if($.runningScene)
            {
                $.disconnect_notice = 1;
                var alert=new dwc.PopupAlert(text);
                $.runningScene.addChild(alert);
                alert.Show();
            }

            //cc.eventManager.dispatchCustomEvent("TONGZHI",obj);
        }catch (e)
        {
            cc.log(e.name + ":" + e.message);
        }

    },

    _userchip : function(__msg){
        dwc.Myself.GetData().SetCoin(__msg.m_lTotalCoin);
        dwc.Myself.GetData().SetGem(__msg.m_lTotalGem);
        if(__msg.m_lTotalDeposit) {
            dwc.Myself.GetData().SetTotalDeposit(__msg.m_lTotalDeposit);
        }
        cc.eventManager.dispatchCustomEvent("CHANGE_PLAYER_INFO");
    },
    _bind_agent : function(_msg)
    {
        try
        {

            var cbResult = _msg.cbResult;
            var agent = _msg.m_lAgent;
            if(agent==undefined)
            {
                var agent = _msg.m_lAgentID;
            }
            if(agent)
            {
                dwc.Myself.GetData().SetAgent(parseInt(agent));
            }
            var totalGem = _msg.m_lTotalGem;

            if(cbResult == 0)
            {
                if(totalGem)
                {
                    dwc.Myself.GetData().SetGem(totalGem);
                }
                if($.runningScene)
                {
                    dwc.Myself.GetData().SetAgent(parseInt(agent));
                var alert = new dwc.PopupAlert("绑定成功，房卡已发放到您的账户");
                    $.runningScene.addChild(alert);
                    alert.Show();
                }
                cc.eventManager.dispatchCustomEvent("CHANGE_PLAYER_INFO");
            }else{
                if($.runningScene)
                {
                    if(cbResult == 1)
                    {
                        var alert = new dwc.PopupAlert("没有找到这个代理商，请咨询代理");
                        $.runningScene.addChild(alert);
                        alert.Show();
                    }else if(cbResult == 2)
                    {
                        var alert = new dwc.PopupAlert("未能成功绑定代理，请咨询代理商");
                        $.runningScene.addChild(alert);
                        alert.Show();
                    }else if(cbResult == 3)
                    {
                        var alert = new dwc.PopupAlert("您已经绑定了代理商，不能更换代理");
                        $.runningScene.addChild(alert);
                        alert.Show();
                    }else
                    {
                        var alert = new dwc.PopupAlert("绑定代理商失败，请输入正确的推荐码");
                        $.runningScene.addChild(alert);
                        alert.Show();
                    }
                }
            }
        }catch(e)
        {
            cc.log(e.name + ":" + e.message);
        }
    },
    sendConverGem2Coin : function(gem)
    {
        var obj =
        {
            gem : gem,
        };
        var that = this;
        that.SendMessage("hall_convert_gem2coin",obj);
    },
    _paysuccess : function(__msg){
        try{
            dwc.Myself.GetData().SetCoin(__msg.m_lTotalCoin);
            dwc.Myself.GetData().SetGem(__msg.m_lTotalGem);

            if(__msg.m_lTotalDeposit) {
                dwc.Myself.GetData().SetTotalDeposit(__msg.m_lTotalDeposit);
            }
            if(__msg.m_iVipLevel) {
                dwc.Myself.GetData().SetVIPLevel(__msg.m_iVipLevel);
            }

            cc.eventManager.dispatchCustomEvent("CHANGE_PLAYER_INFO");
        }catch(e)
        {
            cc.log(e.name + ":" + e.message);
        }
    },
    _convertsuccess : function(__msg){

        dwc.Myself.GetData().SetCoin(__msg.m_lTotalCoin);
        dwc.Myself.GetData().SetGem(__msg.m_lTotalGem);

        if(__msg.m_lTotalDeposit) {
            dwc.Myself.GetData().SetTotalDeposit(__msg.m_lTotalDeposit);
        }
        if(__msg.m_iVipLevel) {
            dwc.Myself.GetData().SetVIPLevel(__msg.m_iVipLevel);
        }

        cc.eventManager.dispatchCustomEvent("CHANGE_PLAYER_INFO");
    },
    _transfer_coin : function(__msg) {
        var cbResult = __msg.cbResult;
        if(cbResult == 0)
        {
            var from_uid = __msg.m_lFromUid;
            var to_uid = __msg.m_lToUid;
            var from_coin = __msg.m_lFromCoin;
            var to_coin = __msg.m_lToCoin;
            if(from_uid == dwc.Myself.GetData().GetUid())
            {
                dwc.Myself.GetData().SetCoin(from_coin);
            }
            if(to_uid == dwc.Myself.GetData().GetUid())
            {
                dwc.Myself.GetData().SetCoin(to_coin);
            }
        }
        if(cbResult == 1)
        {
            if($.runningScene)
            {
                var alert = new dwc.PopupAlert("目标用户不在线，无法赠送");
                $.runningScene.addChild(alert);
                alert.Show();
            }
        }
        if(cbResult == 2)
        {
            if($.runningScene)
            {
                var alert = new dwc.PopupAlert("余额不足，确保赠送后您的余额\n高于5000金币");
                $.runningScene.addChild(alert);
                alert.Show();
            }
        }
    },
    sendAvatar : function(avatar,url)
    {
        var that = this;
        var obj = {};
        obj.avatar = avatar;
        obj.url = url;
        that.SendMessage("hall_avatar",obj);
    },
    gotoStage : function(scene_name)
    {
        var that = this;
        if(scene_name == "XLCH")
        {
            if (xlch.MJ_Data && !xlch.MJ_Data.m_uiGameLayer) {
                xlch.MJ_Data.InitData();
                cc.director.runScene(new xlch.mj_uiGameScene());
            }
        }
        if(scene_name == "NIUNIU")
        {
            niuniu.NN_Data.InitData();
            cc.director.runScene(new niuniu.NNMainScene());
        }
        if(scene_name == "ZJH")
        {
            if(zjh.ZJH_Data && !zjh.ZJH_Data.m_uiGameLayer)
            {
                zjh.ZJH_Data.InitData(1);
                cc.director.runScene(new zjh.ZJHMainScene());
            }
        }
        if(scene_name == "SG")
        {
            if(sg.SG_Data && !sg.SG_Data.m_uiGameLayer)
            {
                sg.SG_Data.InitData(1);
                cc.director.runScene(new sg.NNMainScene());
            }
        }

        if (scene_name == "XLCH")
        {
                if (!xlch.MJ_Data.m_uiGameLayer)
                {
                    xlch.MJ_Data.InitData();
                    cc.director.runScene(new xlch.mj_uiGameScene());
                }
        }
        if (scene_name == "GBMJ")
        {
                if (!gbmj.MJ_Data.m_uiGameLayer)
                {
                    gbmj.MJ_Data.InitData();
                    cc.director.runScene(new gbmj.mj_uiGameScene());
                }
        }
        if (scene_name == "JIHU" || scene_name == "JIPINGHU") {

                if (!gdmj_jipinghu.MJ_Data.m_uiGameLayer)
                {
                    gdmj_jipinghu.MJ_Data.InitData();
                    cc.director.runScene(new gdmj_jipinghu.mj_uiGameScene());
                }

        }
        if (scene_name == "TUIDAOHU") {
                if (!gdmj_tuidaohu.MJ_Data.m_uiGameLayer)
                {
                    gdmj_tuidaohu.MJ_Data.InitData();
                    cc.director.runScene(new gdmj_tuidaohu.mj_uiGameScene());
                }
        }
        if (scene_name == "JHMJ") {
                if (!jhmj.MJ_Data.m_uiGameLayer)
                {
                    jhmj.MJ_Data.InitData();
                    cc.director.runScene(new jhmj.mj_uiGameScene());
                }
        }
        if (scene_name == "CSMJ") {
            if (!csmj.MJ_Data.m_uiGameLayer)
            {
                csmj.MJ_Data.InitData();
                cc.director.runScene(new csmj.mj_uiGameScene());
            }
        }
        if (scene_name == "YNMJ") {
            if (!ynmj.MJ_Data.m_uiGameLayer)
            {
                ynmj.MJ_Data.InitData();
                cc.director.runScene(new ynmj.mj_uiGameScene());
            }

        }
        if (scene_name == "ZSMJ") {

        }
        if (scene_name == "NBMJ") {

        }
        if (scene_name == "SXMJ") {

        }
        if (scene_name == "XJMJ") {
            if (!xjmj.MJ_Data.m_uiGameLayer)
            {
                xjmj.MJ_Data.InitData();
                cc.director.runScene(new xjmj.mj_uiGameScene());
            }
        }
    },
}
dwc.chatMessageListArray=[];
